﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using C1.Phone.Chart;
using Microsoft.Phone.Shell;

namespace ControlExplorer.Controls.Chart
{
    public partial class Animation
        : UserControl, IExposeApplicationBarItems
    {
        Dictionary<string, PlotElement> _dict = new Dictionary<string, PlotElement>();

        // Constructor
        public Animation()
        {
            InitializeComponent();

            Loaded += Animation_Loaded;
        }

        void Animation_Loaded(object sender, RoutedEventArgs e)
        {
            chart.View.AxisY.Min = 0;
            chart.View.AxisY.Max = 100;
            BarColumnOptions.SetRadiusX(chart, 2);
            BarColumnOptions.SetRadiusY(chart, 2);
            chart.ChartType = ChartType.Column;

            NewData();
        }

        Random rnd = new Random();

        void NewData()
        {
            // create new random data values
            int cnt = 12;
            double[] vals = new double[cnt];
            for (int i = 0; i < cnt; i++)
                vals[i] = rnd.Next(10, 100);

            DataSeries ds;
            if (chart.Data.Children.Count == 0)
            {
              ds = new DataSeries();
                ds.PlotElementLoaded += ds_PlotElementLoaded;
                chart.Data.Children.Add(ds);
            }
            chart.Data.Children[0].ValuesSource = vals;
        }

        void ds_PlotElementLoaded(object sender, EventArgs e)
        {
            var pe = (PlotElement)sender;
            string key = GetKey(pe);
            if (_dict.ContainsKey(key))
            {
                var pe0 = _dict[key];
                Rect rc0 = ((Path)pe0.Shape).Data.Bounds;
                Rect rc = ((Path)pe.Shape).Data.Bounds;

                if (rc0 != rc) // different values
                {
                    // transform to fit into the previous value
                    ScaleTransform st = new ScaleTransform()
                    {
                        ScaleX = rc0.Width / rc.Width,
                        ScaleY = rc0.Height / rc.Height
                    };
                    pe.RenderTransform = st;
                    pe.RenderTransformOrigin = new Point(0, 1);

                    // animate to the current value
                    Storyboard sb = new Storyboard() { Duration = new Duration(TimeSpan.FromSeconds(3)) };
                    DoubleAnimation da1 = new DoubleAnimation() { To = 1.0 };
                    Storyboard.SetTarget(da1, st);
                    Storyboard.SetTargetProperty(da1, new PropertyPath("ScaleX"));
                    sb.Children.Add(da1);
                    DoubleAnimation da2 = new DoubleAnimation() { To = 1.0 };
                    Storyboard.SetTarget(da2, st);
                    Storyboard.SetTargetProperty(da2, new PropertyPath("ScaleY"));
                    sb.Children.Add(da2);
                    sb.Begin();
                }
            }
            // store the previous element into the dictionary
            _dict[key] = pe;
            pe.Opacity = 1.0;
        }

        static string GetKey(PlotElement pe)
        {
            return string.Format("s{0}p{1}", pe.DataPoint.SeriesIndex, pe.DataPoint.PointIndex);
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            NewData();
        }

    
        #region IExposeApplicationBarItems Members

        public IEnumerable<IApplicationBarMenuItem>  ApplicationBarItems
        {
	        get 
            { 
                var btn = new ApplicationBarIconButton()
                {
                    IconUri = new Uri("/Resources/AppBar/plus.png", UriKind.Relative),
                    Text = "New Data",
                    IsEnabled = true
                };
                btn.Click += btnNew_Click;

                yield return btn;
            }
        }

        #endregion

    }
}
